package org.flyby;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.flyby.api.persistence.Cart;
import org.flyby.component.FlybyIndicatingAjaxLink;
import org.flyby.component.FlybyIndicatingAjaxSubmitLink;
import org.flyby.component.FlybyPage;

/**
 * Created with IntelliJ IDEA.
 * User: Max
 */
public class CheckoutPage extends FlybyPage {

    private String creditCardNumber = "1234123412341234";
    private String creditCardOwnerName = "Yehuda.Z";
    private String expirationDateStr = "01/19";

    public CheckoutPage(Cart cart) {
        super(new CompoundPropertyModel<Cart>(cart));

        final Form creditCardDetailsForm = new Form("creditCardDetailsForm");
        add(creditCardDetailsForm);
        final WebMarkupContainer creditCardDetailsContainer = new WebMarkupContainer("creditCardDetailsContainer");
        creditCardDetailsContainer.setOutputMarkupPlaceholderTag(true);
        creditCardDetailsForm.add(creditCardDetailsContainer);
        creditCardDetailsContainer.add(new TextField<String>("creditCardNumber", PropertyModel.<String>of(this, "creditCardNumber")));
        creditCardDetailsContainer.add(new TextField<String>("creditCardOwnerName", PropertyModel.<String>of(this, "creditCardOwnerName")));
        creditCardDetailsContainer.add(new TextField<String>("expirationDateStr", PropertyModel.<String>of(this, "expirationDateStr")));


        creditCardDetailsForm.add(new FlybyIndicatingAjaxSubmitLink("selfCheckout") {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                super.onSubmit(target, form);
            }
        });

        add(new FlybyIndicatingAjaxLink("superCheckout") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                Cart cart = (Cart) CheckoutPage.this.getDefaultModelObject();

                String cartDetails = "Weight:" + cart.getTotalWeight().toString() + ", Price:" + +cart.getTotalPrice();

                target.appendJavaScript("encodeText('" + cartDetails + "')");
            }
        });

    }

    private void generateQrBarcode() {

    }
}
